﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Web;
using System.Collections;
using System.Web.UI;
using System.Web.UI.WebControls;
using BLL;
using Factory;

namespace test806
{
    public partial class WebForm1 : System.Web.UI.Page
    {
        //private string DataSource_str1 = "Data Source=cntsnwa025737\\sql2008r2;Initial Catalog=TrainingWorkShop;Integrated Security=True ";
        private IOperateDBRepository lg = OperateDBFactory.InitializeOperateDBRepository();
        private static  Role role;
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                string roleid_temp = Request.QueryString["roleid_trans"];//接收前一个页面的数据
                int role_Id = Convert.ToInt32(roleid_temp);//转化成int类型

                this.CheckBoxList1.DataSource = lg.GetAuthority();
                this.CheckBoxList1.DataTextField = "AuthorityName";
                this.CheckBoxList1.DataValueField = "AuthorityId";
                this.CheckBoxList1.DataBind();


                if (role_Id == 0)
                {
                    
                }
                else
                {
                    
                    if (lg.GetRoleById(role_Id) == null)
                    {
                       
                    }
                    else
                    {
                        role = lg.GetRoleById(role_Id);
                        int selectStatusIdResult = (int)(role.StatusId);//返回只有一条记录的查询
                        //以下把读出来的statusid显示到页面
                        if (selectStatusIdResult == 1)
                        {
                            RadioButton1.Checked = true;
                        }
                        if (selectStatusIdResult == 2)
                        {
                            RadioButton2.Checked = true;
                        }
                        RoleName.Text = role.RoleName; ; //给textbox赋值
                        int authority_count = CheckBoxList1.Items.Count;//数组的大小是权限个数，但是数组本身的大小应该是选中的权限的数量
                        ArrayList authorityaalllist = new ArrayList();//用 arraylist来存放查出来的数据
                        for (int i = 0; i < role.Authorities.Count; i++)
                        {
                            authorityaalllist.Add(role.Authorities.ElementAt(i).AuthorityId.ToString());

                        }                
                        //将authorityid存在于arraylist中的authority选出来
                        for (int q = 0; q < CheckBoxList1.Items.Count; q++)
                        {
                            if (authorityaalllist.IndexOf(CheckBoxList1.Items[q].Value) != -1)
                            { CheckBoxList1.Items[q].Selected = true; }

                        }
                    }
                }
            }
        }
 

        protected void Button1_Click(object sender, EventArgs e)
        {

            string roleid_temp = Request.QueryString["roleid_trans"];//接收前一个页面的数据
            int role_Id = Convert.ToInt32(roleid_temp);//转化成int类型

            if (role_Id > 0)
            {
                role.RoleName = RoleName.Text;
                if (RadioButton1.Checked)
                {
                    role.StatusId = RoleStatus.InUse;
                }
                if (RadioButton2.Checked)
                {
                    role.StatusId = RoleStatus.NoUse;
                }
                role.Authorities.Clear();
                int count = CheckBoxList1.Items.Count;
                for (int i = 0; i < count; i++)
                {
                    if (CheckBoxList1.Items[i].Selected)
                    {
                        Authority authority = new Authority();
                        authority.AuthorityName = CheckBoxList1.Items[i].Text;
                        authority.AuthorityId = Convert.ToInt32(CheckBoxList1.Items[i].Value);
                        role.Authorities.Add(authority);
                    }
                }

                lg.UpdateRole(role);
            }
                //以下是新增role功能

                if (role_Id == 0)
                {
                    role = new Role();
                    role.RoleName = RoleName.Text;

                    if (RadioButton1.Checked)
                    {
                        role.StatusId = RoleStatus.InUse;
                    }
                    if (RadioButton2.Checked)
                    {
                        role.StatusId = RoleStatus.NoUse;
                    }
                    role.Authorities = new List<Authority>();
                  
                    int count = CheckBoxList1.Items.Count;
                    for (int i = 0; i < count; i++)
                    {
                        if (CheckBoxList1.Items[i].Selected)
                        {
                            Authority authority = new Authority();
                            authority.AuthorityName = CheckBoxList1.Items[i].Text;
                            authority.AuthorityId = Convert.ToInt32(CheckBoxList1.Items[i].Value);
                            role.Authorities.Add(authority);
                        }
                    }
                    lg.InsertRole(role);
                }
                    //以下是弹出对话框
                Response.Write("<script>alert('Success!')</script>");  
        }
        protected void Admin4(object sender, EventArgs e)
        {
        }

        protected void CheckBoxList1_SelectedIndexChanged(object sender, EventArgs e)
        {

        }

    }
}
